package com.ssps.front.tenant2.controller2;

import com.ssps.entity.Role;
import com.ssps.entity.User;
import com.ssps.front.tenant2.service2.Tenant2SignService;
import com.ssps.front.tenant2.utils2.Tenant2Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: peng
 */
@Controller
@RequestMapping("sign2")
public class Tenant2SignController {
    @Autowired
    Tenant2SignService signService;



    @RequestMapping("initAddUser2")
    @ResponseBody

    public Map<String,Object> initAddUser2(){
        Map<String,Object> map = new HashMap<>();
        try{
            List<Role> roles = signService.initAddUser2(null);
            for(int i = 0 ; i < roles.size() ; i++){
                if(roles.get(i).getRname().equals("平台") ){
                    roles.remove(i);
                }
                if(roles.get(i).getRname().equals("物业")){
                    roles.remove(i);
                }
            }

            map.put("roles",roles);
            map.put("result",200);
        }catch (Exception e){
            map.put("result",220);
        }finally {
            return map;
        }
    }


    @RequestMapping("addUser2")
    @ResponseBody

    public Map<String,Object> addUser2(@RequestBody String body){
        Map<String,Object> map = new HashMap<>();
        Map<String, String> parse = Tenant2Utils.parse(body);
        //System.out.println(parse);
        User user = new User();
        user.setAcount(parse.get("acount"));
        user.setPhone(parse.get("phone"));
        user.setPwd(parse.get("pwd"));
        try{
            Integer flag = signService.addUser(user,parse.get("rid"),parse.get("othercode"));
           map.put("result",flag);
        }catch (Exception e){
            map.put("result",220);
        }finally {
            return map;
        }
    }

}
